gcc在Linux下如何运行一个C/C++程序 | 您所在的位置:网站首页 › linux c++ 读取txt文件 › gcc在Linux下如何运行一个C/C++程序 |
安装gcc:sudo apt-get install gcc(之后输入密码即可)
绝对路径的方式进入usr目录: cd /home /home/:是普通用户的主目录,在创建用户时,每个用户要有一个默认登录和保存自己数据的位置,就是用户的主目录,所有普通用户的主目录是在/home/ 下建立一个和用户名相同的目录。如:user1的主目录就是/home/user1/。创建用户名为user1的目录:sudo mkdir user1(输入密码即可)进入user1目录:cd user1建立存放代码数据的文件:mkdir codes进入codes目录:cd codes演示在Ccode目录下进行,实验环境为 实体机CentOS7.3 gcc 4.8.5首先建立test1.c文件写入C程序 使用以下命令 vim test1.c执行完成后按i进入编辑模式 #include int main(){ int a = 0; printf("测试用例%d\n",a); return 0; } 分步进行1)预处理,2)编译,3)汇编,4)链接(1)预处理:gcc -E -o test1.i test1.c (2)编译: gcc -S -o test1.s test1.i (3)汇编:gcc -c -o test1.o test1.s (4)链接:gcc -o test1test1.o (5)执行可执行文件helloworld或a.out: 之后使用 ./test1即可运行程序 以下为可写成Linux shell脚本进行这个过程 # build脚本 gcc -E -o test1.i test1.c gcc -S -o test1.s test1.i gcc -c -o test1.o test1.s gcc -o test1test1.o建立好脚本build 后需要对其赋予执行权限 chomd +x build使用ls命令查看 执行test1结果 不建议使用root用户进行操作该文章仅供学习参考 |
CopyRight 2018-2019 实验室设备网 版权所有 |